{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f09df847",
   "metadata": {},
   "source": [
    "# SQL formatting"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4ebd8538",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "To enable SQL formatting, install `jupysql`:\n",
    "\n",
    "```sh\n",
    "pip install jupysql --upgrade\n",
    "```\n",
    "\n",
    "Then, a \"Format SQL\" button will appear in JupyterLab:\n",
    "\n",
    "![format](../static/format-sql.gif)\n",
    "\n",
    "\n",
    "Click on \"Format SQL\" and you'll see that the SQL cell below is formatted!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aa377aa3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "from urllib.request import urlretrieve\n",
    "\n",
    "if not Path(\"penguins.csv\").is_file():\n",
    "    urlretrieve(\n",
    "        \"https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv\",\n",
    "        \"penguins.csv\",\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3d31021c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<span style=\"None\">Tip: You may define configurations in /Users/neelashasen/Dev/jupysql_master/jupysql/pyproject.toml or /Users/neelashasen/.jupysql/config. </span>"
      ],
      "text/plain": [
       "Tip: You may define configurations in /Users/neelashasen/Dev/jupysql_master/jupysql/pyproject.toml or /Users/neelashasen/.jupysql/config. "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "Please review our <a href='https://jupysql.ploomber.io/en/latest/api/configuration.html#loading-from-a-file'>configuration guideline</a>."
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<span style=\"None\">Loading configurations from /Users/neelashasen/.jupysql/config.</span>"
      ],
      "text/plain": [
       "Loading configurations from /Users/neelashasen/.jupysql/config."
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<span style=\"None\">Settings changed:</span>"
      ],
      "text/plain": [
       "Settings changed:"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "<table>\n",
       "    <thead>\n",
       "        <tr>\n",
       "            <th>Config</th>\n",
       "            <th>value</th>\n",
       "        </tr>\n",
       "    </thead>\n",
       "    <tbody>\n",
       "        <tr>\n",
       "            <td>feedback</td>\n",
       "            <td>True</td>\n",
       "        </tr>\n",
       "        <tr>\n",
       "            <td>autopandas</td>\n",
       "            <td>True</td>\n",
       "        </tr>\n",
       "    </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "\n",
       "+------------+-------+\n",
       "|   Config   | value |\n",
       "+------------+-------+\n",
       "|  feedback  |  True |\n",
       "| autopandas |  True |\n",
       "+------------+-------+"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<span style=\"None\">Connecting to &#x27;default&#x27;</span>"
      ],
      "text/plain": [
       "Connecting to 'default'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<span style=\"None\">Connecting and switching to connection &#x27;duckdb://&#x27;</span>"
      ],
      "text/plain": [
       "Connecting and switching to connection 'duckdb://'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%load_ext sql\n",
    "%sql duckdb://"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4185be28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<span style=\"None\">Running query in &#x27;duckdb://&#x27;</span>"
      ],
      "text/plain": [
       "Running query in 'duckdb://'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>species</th>\n",
       "      <th>island</th>\n",
       "      <th>bill_length_mm</th>\n",
       "      <th>bill_depth_mm</th>\n",
       "      <th>flipper_length_mm</th>\n",
       "      <th>body_mass_g</th>\n",
       "      <th>sex</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>39.1</td>\n",
       "      <td>18.7</td>\n",
       "      <td>181</td>\n",
       "      <td>3750</td>\n",
       "      <td>MALE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>39.5</td>\n",
       "      <td>17.4</td>\n",
       "      <td>186</td>\n",
       "      <td>3800</td>\n",
       "      <td>FEMALE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Adelie</td>\n",
       "      <td>Torgersen</td>\n",
       "      <td>40.3</td>\n",
       "      <td>18.0</td>\n",
       "      <td>195</td>\n",
       "      <td>3250</td>\n",
       "      <td>FEMALE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  species     island  bill_length_mm  bill_depth_mm  flipper_length_mm  \\\n",
       "0  Adelie  Torgersen            39.1           18.7                181   \n",
       "1  Adelie  Torgersen            39.5           17.4                186   \n",
       "2  Adelie  Torgersen            40.3           18.0                195   \n",
       "\n",
       "   body_mass_g     sex  \n",
       "0         3750    MALE  \n",
       "1         3800  FEMALE  \n",
       "2         3250  FEMALE  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "select * from penguins.csv where island = 'Torgersen' limit 3"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "notebook_metadata_filter": "myst"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  },
  "myst": {
   "html_meta": {
    "description lang=en": "Format your SQL cells in Jupyter",
    "keywords": "jupyter, jupyterlab, sql",
    "property=og:locale": "en_US"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
